import { parseUser } from './user';

export * from './construct';
export * from './user';

const store = {
    state: {},
    env: {},
    app: {
        title: '',
        tabNavs: [],
        pages: {},
        user: {}
    },

    http: {
        baseURL: '',
        // 请求模式，ajax fetch axios
        model: 'ajax',
        // 请求超时时间(毫秒)
        timeout: 0,
        // 请求重试次数
        retry: 3,
        
        /**
         * @property {(): Record<string, string | boolean | number>} 请求时获取公共参数的方法
         **/
        onGetRequestCommonFields: null,

        /**
         * 请求响应后优先调用
         */
        onBeforeResponse: null
    }
};

export const setStore = (name, value) => store.state[name] = value;
export const getStore = name => store.state[name];

export const getEnv = () => store.env;

export const setAppInfo = (name, value) => store.app = value;
export const getAppInfo = name => store.app[name];
export const setHttpInfo = (name, value) => store.http[name] = value;
export const getHttpInfo = name => store.http[name];
export const getHttp = () => ({ ...store.http });

export const init = (options) => {
    store.env = options.env;
    store.app.title = options.title;
    store.app.pages = options.pages;
    store.app.tabNavs = options.tabNavs;
    store.app.user = parseUser(options.baseUser, options.currentOrgId);
    
    store.http.getRequestCommonFields = options.getRequestCommonFields || null;
};

